/*=============================================================================

  APPLICATION MANAGER MODULE

  FILE: UImgr.cpp
  
  DESCRIPTION

    Manager Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include "UImgr_MOD.h"
#include "dlgEng.h"

/*=============================================================================
  DEVICE STATUS DEFINITION
=============================================================================*/
#define DEV_STATUS_GPROGVW

typedef struct
{
	dword	type;
	dword	event;
	dword	item_id;
	char		str[64];
} dev_prog_s;

static dev_prog_s gEvt_prog[DEVINIT_MAX*2*3] = 
{
	// AREA_MOD
	{AREA_MOD, INIT_START_E, DEVINIT_MOD, "Process loading ..."},
	{AREA_MOD, INIT_DONE_E, DEVINIT_MOD,   "Process loading completed ..."},
	{AREA_MOD, ERR_INIT_E, DEVINIT_MOD,  "Process loading failed ..."},

	{AREA_MOD, CLOSE_START_E, DEVCLOSE_MOD,  "Process unloading ..."},
	{AREA_MOD, CLOSE_DONE_E, DEVCLOSE_MOD,  "Process unloading completed ..."},
	{AREA_MOD, ERR_CLOSE_E, DEVCLOSE_MOD,   "Process unloading failed ..."},
	// HIO_DEV
	{HIO_DEV, INIT_START_E, DEVINIT_HDIO, "Digital IO initialization ..."},
	{HIO_DEV, INIT_DONE_E, DEVINIT_HDIO,   "Digital IO initialization completed ..."},
	{HIO_DEV, ERR_INIT_E, DEVINIT_HDIO,  "Digital IO initialization failed ..."},

	{HIO_DEV, CLOSE_START_E, DEVCLOSE_HDIO,  "Digital IO closing ..."},
	{HIO_DEV, CLOSE_DONE_E, DEVCLOSE_HDIO,  "Digital IO closing completed ..."},
	{HIO_DEV, ERR_CLOSE_E, DEVCLOSE_HDIO,   "Digital IO closing failed ..."},
	// HAIO_DEV
	{HAIO_DEV, INIT_START_E, DEVINIT_HAIO, "Analog IO initialization ..."},
	{HAIO_DEV, INIT_DONE_E, DEVINIT_HAIO,   "Analog IO initialization completed ..."},
	{HAIO_DEV, ERR_INIT_E, DEVINIT_HAIO,  "Analog IO initialization failed ..."},

	{HAIO_DEV, CLOSE_START_E, DEVCLOSE_HAIO,  "Analog IO closing ..."},
	{HAIO_DEV, CLOSE_DONE_E, DEVCLOSE_HAIO,  "Analog IO closing completed ..."},
	{HAIO_DEV, ERR_CLOSE_E, DEVCLOSE_HAIO,   "Analog IO closing failed ..."},
	// HMOT_DEV
	{HMOT_DEV, INIT_START_E, DEVINIT_HMOT, "Motion device initialization ..."},
	{HMOT_DEV, INIT_DONE_E, DEVINIT_HMOT,   "Motion device initialization completed ..."},
	{HMOT_DEV, ERR_INIT_E, DEVINIT_HMOT,  "Motion device initialization failed ..."},

	{HMOT_DEV, CLOSE_START_E, DEVCLOSE_HMOT,  "Motion device closing ..."},
	{HMOT_DEV, CLOSE_DONE_E, DEVCLOSE_HMOT,  "Motion device closing completed ..."},
	{HMOT_DEV, ERR_CLOSE_E, DEVCLOSE_HMOT,   "Motion device closing failed ..."},
	// HFG_DEV
	{HFG_DEV, INIT_START_E, DEVINIT_HFG, "Frame Grabber initialization ..."},
	{HFG_DEV, INIT_DONE_E, DEVINIT_HFG,   "Frame Grabber initialization completed ..."},
	{HFG_DEV, ERR_INIT_E, DEVINIT_HFG,  "Frame Grabber initialization failed ..."},

	{HFG_DEV, CLOSE_START_E, DEVCLOSE_HFG,  "Frame Grabber closing ..."},
	{HFG_DEV, CLOSE_DONE_E, DEVCLOSE_HFG,  "Frame Grabber closing completed ..."},
	{HFG_DEV, ERR_CLOSE_E, DEVCLOSE_HFG,   "Frame Grabber closing failed ..."},
	// HCAM_DEV
	{HCAM_DEV, INIT_START_E, DEVINIT_HCAM, "Camera Interface initialization ..."},
	{HCAM_DEV, INIT_DONE_E, DEVINIT_HCAM,   "Camera Interface initialization completed ..."},
	{HCAM_DEV, ERR_INIT_E, DEVINIT_HCAM,  "Camera Interface initialization failed ..."},

	{HCAM_DEV, CLOSE_START_E, DEVCLOSE_HCAM,  "Camera Interface closing ..."},
	{HCAM_DEV, CLOSE_DONE_E, DEVCLOSE_HCAM,  "Camera Interface closing completed ..."},
	{HCAM_DEV, ERR_CLOSE_E, DEVCLOSE_HCAM,   "Camera Interface closing failed ..."},
};

/*=============================================================================
  STATE UI DEFINITION
=============================================================================*/
typedef struct
{
	dword	maj_sts;
	dword	min_sts;
	char		maj_str[32];
	char		min_str[64];
} state_string_s;

static state_string_s gSts_str[] = 
{
	// IDL_S
	{IDL_S, IDL_IDL_S, "Idle status",  "Idle..."},
	{IDL_S, IDL_ERR_S, "Error",  "Check for error !"},

	// DEV_S
	{DEV_S, DEV_IDL_S, "Reset system",  ""},
	{DEV_S, DEV_INIT_S, "Reset system",  "Initializing system.."},
	{DEV_S, DEV_CLOSE_S, "Reset system",  "Terminating system.."},

	// RESET_S
	{RESET_S, RESET_IDL_S, "Reset system",  ""},
	{RESET_S, RESET_RESET_S, "Reset system",  "System resetting.."},
	{RESET_S, RESET_HOME_S, "Reset system", "Homming system.. "},
	
	// MANUAL_S
	{MANUAL_S, MANUAL_IDL_S, "Manual",  ""},
	{MANUAL_S, MANUAL_AUTO_S, "Manual",  "Auto Measuring.."},
	{MANUAL_S, MANUAL_READ_S, "Manual",  "Realtime Measuring.."},
};

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/
extern void vwTitle_Text(const char *pszTitle);
extern void vwTitle_Show(boolean enable);

static void ui_sysmgr_event(dword event, dword wp, dword lp);
static void ui_sysmgr_link(void);
static void ui_sysmgr_prep_noti(dword new_sts, dword old_sts);
static void ui_dev_event(dword type, dword event, dword wp, dword lp);
static void ui_sysmgr_state(void);

/*=============================================================================
  EXTERNALIZED FUNCTION DEFINITION
=============================================================================*/
boolean ui_event_proc(dword type, dword event, dword wp, dword lp)
{
	CMainView		*vw = (CMainView*)ObjList_Get(VIEW_MAIN);
	CDlgEng			*dlg = (CDlgEng*)ObjList_Get(VIEW_ENG);
	dword			evt, evt_id;

	evt = HDEV_GETEVENT(event);
	evt_id = HDEV_GETDEV_ID(event);

	// Device relation event
	switch(evt)
	{
	// SYSmgr
	case DEVINIT_START_E:
	case DEVINIT_DONE_E:
	case ERR_DEVINIT_E:
	case DEVCLOSE_START_E:
	case DEVCLOSE_DONE_E:
	case ERR_DEVCLOSE_E:
	// Device
	case INIT_START_E:
	case INIT_DONE_E:
	case CLOSE_START_E:
	case CLOSE_DONE_E:
	case ERR_INIT_E:
	case ERR_CLOSE_E:
	{
		ui_dev_event(type, event, wp, lp);
		break;
	}
	}

	switch(type)
	{
	case SYS_MGR:
	{
		ui_sysmgr_event(event, wp, lp);
		break;
	}
	case UI_DEV:
	{
		switch(evt)
		{
		case UI_VWCHANGE_E:
		{
			UIbtn_MainOP(UI_DEV, event);
			break;
		}
		case UI_UPDATE_E:
		{
			UIapp_Update();
			break;
		}
		}
		break;
	}
	}

	if(dlg)
	{
		dlg->Event_Handler(type, event, wp, lp);
	}
	if(vw)
	{
		vw->Event_Handler(type, event, wp, lp);
	}

	return	TRUE;
}

/*=============================================================================
  LOCALIZED FUNCTION DEFINITION
=============================================================================*/
/*-----------------------------------------------
  SYSmgr EVENT
-----------------------------------------------*/
void ui_sysmgr_event(dword event, dword wp, dword lp)
{
	SYSmgr			*sm = (SYSmgr*)ObjList_Get(SYS_MGR);
	dword			evt, evt_id;

	evt = HDEV_GETEVENT(event);
	evt_id = HDEV_GETDEV_ID(event);
	
	switch(evt)
	{
	case INIT_DONE_E:
	{
		ui_sysmgr_link();
		break;
	}
	case DEVINIT_START_E:
	{
		Vmain				*vm = (Vmain*)ObjList_Get(VIEW_VMAIN);

		// Job 
		vm->UI_SYNC(TRUE);
		
		UIvw_Select(UI_DEVINIT_VW);
		break;
	}
	case DEVCLOSE_START_E:
	{
		UIvw_Select(UI_DEVCLOSE_VW);
		break;
	}
	case DEVINIT_DONE_E:
	{
		UIvw_Select(UI_MAIN_VW);
		break;
	}
	case ERR_DEVINIT_E:
	{
		NBOX_REQ("Error", "Error while initializing system", 3);
		UIvw_Select(UI_MAIN_VW);
		break;
	}
	case SYSPREP_UPDATE_E:
	{
		ui_sysmgr_prep_noti(wp, lp);
		UIbtn_MainOP(SYS_MGR, event);
		break;
	}
	case CHANGE_MAJSTATE_E:
	case CHANGE_MINSTATE_E:
	{
		CMainView		*vw = (CMainView*)ObjList_Get(VIEW_MAIN);
	
		ui_sysmgr_state();
		UIbtn_MainOP(SYS_MGR, event);
		UIapp_Update();
		break;
	}
	}
}

/*-----------------------------------------------
  SYSmgr Link
-----------------------------------------------*/
void ui_sysmgr_link(void)
{
	SYSmgr		*sm = (SYSmgr*)ObjList_Get(SYS_MGR);
	CMainView	*mv = (CMainView*)ObjList_Get(VIEW_MAIN);
	char			*str;
	
	if(sm == NULL)
		return;

	str = UIstring(NULL);
	
	//-------------------------------------------
	// PARAMETER RETRIEVE
	//-------------------------------------------
	{
		// application manager
		memZeros(str, UI_STRING_LEN);
		if(UIprm_Get(UI_SYSMGR_SP, (dword)str, UI_STRING_LEN) == TRUE)
		{
			sm->CFG_Setup(str);
		}

		// UI_LINK !
		{

		}
	}

	UIstring(str);
}

/*-----------------------------------------------
  SYSmgr PREP NOTI.
-----------------------------------------------*/
void ui_sysmgr_prep_noti(dword new_sts, dword old_sts)
{
	CMainView		*mv = (CMainView*)ObjList_Get(VIEW_MAIN);

	if(mv == NULL)
		return;

}

/*-----------------------------------------------
  SYSmgr STATE UI
-----------------------------------------------*/
void ui_sysmgr_state(void)
{
	state_string_s		*ss = &gSts_str[0];
	SYSmgr			*sm = (SYSmgr*)ObjList_Get(SYS_MGR);
	CMainView		*mv = (CMainView*)ObjList_Get(VIEW_MAIN);
	CFormSts		*fs;
	int32			i, n;
	dword			mj_sts, mn_sts;

	if((mv == NULL) || (sm == NULL))
		return;

	fs = &mv->m_frmSts;

	if(::IsWindow(fs->m_hWnd) == FALSE)
		return;

	mj_sts = sm->State_MajCurrent();
	mn_sts = sm->State_MinCurrent();
	
	n = sizeof(gSts_str) / sizeof(gSts_str[0]);
	for(i=0; i<n; i++, ss++)
	{
		if((ss->maj_sts == mj_sts) && (ss->min_sts == mn_sts))
		{
			// Display
			fs->UI_STATE_MAJOR(ss->maj_str);
			fs->UI_STATE_MINOR(ss->min_str);
			return;
		}
	}
	// Clear 
	fs->UI_STATE_MAJOR("");
	fs->UI_STATE_MINOR("");
}

/*-----------------------------------------------
  SYSmgr DEVICE UI
-----------------------------------------------*/
#ifdef DEV_STATUS_GPROGVW
//GPorvVw Usage
void ui_dev_event(dword type, dword event, dword wp, dword lp)
{
	dev_prog_s	*dp = &gEvt_prog[0];	
	GProgVw		*pv = (GProgVw*)ObjList_Get(VIEW_DEVINIT);
	DWORD		evt, evt_id;
	int32		i, n, cur_id = -1;
	double64		max_id;
	int32		progv, step = -1;
	
	evt = HDEV_GETEVENT(event);
	evt_id = HDEV_GETDEV_ID(event);

	if(pv == NULL)
		return;

	if(type == SYS_MGR)
	{
		switch(evt)
		{
		case DEVINIT_DONE_E:
		case DEVCLOSE_DONE_E:
			break;
		}
		return;
	}
	
	n = sizeof(gEvt_prog)/sizeof(gEvt_prog[0]);
	for(i=0; i<n; i++, dp++)
	{
		if(dp->type != type)
			continue;
		
		if(dp->event != evt)
			continue;

		cur_id = dp->item_id;
		pv->Text_Set(cur_id, dp->str);
		switch(evt)
		{
		case INIT_START_E:
		case CLOSE_START_E:
		{
			pv->Led_Set(cur_id,  GLED_YELLOW);
			step = (cur_id+1)*2 -1;
			break;
		}
		case INIT_DONE_E:
		case CLOSE_DONE_E:
		{
			pv->Led_Set(cur_id, GLED_GREEN);
			step = (cur_id+1)*2;
			break;
		}
		case ERR_INIT_E:
		case ERR_CLOSE_E:
		{
			pv->Led_Set(cur_id, GLED_RED);
			break;
		}
		}
		pv->Item_Show(cur_id, TRUE);		
		break;
	}

	if((cur_id < 0) || (step < 0))
		return;

	max_id = (double64)DEVINIT_MAX*2.0;
	progv = (int32)(((double64)step/max_id)*100.0);

	pv->Prog_Set(progv);
}

#else // DEV_STATUS_GPROGVW

// Just displa in DBGPRINT()!!
void ui_dev_event(dword type, dword event, dword wp, dword lp)
{
	dev_prog_s	*dp = &gEvt_prog[0];	
	DWORD		evt, evt_id;
	int32		i, n, cur_id = -1;
	double64		max_id;
	int32		progv, step = -1;
	
	evt = HDEV_GETEVENT(event);
	evt_id = HDEV_GETDEV_ID(event);

	if(type == SYS_MGR)
	{
		switch(evt)
		{
		case DEVINIT_DONE_E:
		{
			DBGPRINT("[SYSmgr] Device init. done !\n");
			break;
		}
		case DEVCLOSE_DONE_E:
		{
			DBGPRINT("[SYSmgr] Device close done !\n");
			break;
		}
		}
		return;
	}
	
	n = sizeof(gEvt_prog)/sizeof(gEvt_prog[0]);
	for(i=0; i<n; i++, dp++)
	{
		if(dp->type != type)
			continue;
		
		if(dp->event != evt)
			continue;

		cur_id = dp->item_id;
		switch(evt)
		{
		case INIT_START_E:
		case CLOSE_START_E:
		{
			step = (cur_id+1)*2 -1;
			break;
		}
		case INIT_DONE_E:
		case CLOSE_DONE_E:
		{
			step = (cur_id+1)*2;
			break;
		}
		case ERR_INIT_E:
		case ERR_CLOSE_E:
		{
			step = 0;
			break;
		}
		}
		break;
	}

	if((cur_id < 0) || (step < 0))
		return;

	max_id = (double64)DEVINIT_MAX*2.0;
	progv = (int32)(((double64)step/max_id)*100.0);

	DBGPRINT("[%d] %s / %d%%\n", cur_id, dp->str, progv);
}

#endif //DEV_STATUS_GPROGVW

/*--- END OF UImgr_EVT_ui.CPP ---*/
